{%     set loopback_ip_list = device_abstract_config.get('system', {}).get('loopback_ip_list', []) %}
{%     if loopback_ip_list|length > 0 %}
{%         set loopback_ip = loopback_ip_list[0] %}
{%         if loopback_ip %}
set groups {{cfg_group}} routing-options router-id {{loopback_ip}}
set groups {{cfg_group}} routing-options route-distinguisher-id {{loopback_ip}}
{%         endif %}
{%     endif %}

{%     for bgp in device_abstract_config.get('bgp', []) %}
{%         if feature in bgp.get('comment') %}
set groups {{cfg_group}} routing-options resolution rib bgp.rtarget.0 resolution-ribs inet.0
set groups {{cfg_group}} routing-options autonomous-system {{bgp.autonomous_system}}
{%             set contrail_asn_group = '_contrail_asn-'+(bgp.autonomous_system|string) %}
set groups {{cfg_group}} routing-options dynamic-tunnels {{contrail_asn_group}} source-address {{bgp.get('ip_address')}}
{%             set encap_priorities = device_abstract_config.get('system', {}).get('encapsulation_priorities', []) %}
{%             set mode = [] %}
{%             for encap_mode in encap_priorities %}
{%                 if encap_mode != 'VXLAN' %}
{%                     do mode.append(encap_mode) %}
{%                     break %}
{%                 endif %}
{%             endfor %}
{%             if mode|length > 0 and mode[0] == 'MPLSoUDP' %}
set groups {{cfg_group}} routing-options dynamic-tunnels {{contrail_asn_group}} udp
set groups {{cfg_group}} protocols bgp group {{contrail_asn_group}} export mpls_over_udp
set groups {{cfg_group}} protocols bgp group {{contrail_asn_group}} vpn-apply-export
set groups {{cfg_group}} policy-options policy-statement mpls_over_udp term 1 then community add encap-udp
set groups {{cfg_group}} policy-options policy-statement mpls_over_udp term 1 then accept
set groups {{cfg_group}} policy-options community encap-udp members 0x030c:64512:13
{%             else %}
set groups {{cfg_group}} routing-options dynamic-tunnels {{contrail_asn_group}} gre
{%             endif %}
{%             for peer in bgp.get('peers', []) %}
set groups {{cfg_group}} protocols bgp group {{contrail_asn_group}} neighbor {{peer.get('ip_address')}} peer-as {{peer.get('autonomous_system')}} 
{%             endfor %}
{%             set system_info = device_abstract_config.get('system') %}
{%             for net in system_info.get('tunnel_destination_networks', []) %}
set groups {{cfg_group}} routing-options dynamic-tunnels {{contrail_asn_group}} destination-networks {{net.get('prefix')}}/{{net.get('prefix_len')}}
{%             endfor %}
set groups {{cfg_group}} protocols bgp group {{contrail_asn_group}} type {{bgp.get('type_')}}
{%             set cluster_id = bgp.get('cluster_id') %}
{%             if cluster_id != None %}
set groups {{cfg_group}} protocols bgp group {{contrail_asn_group}} cluster {{cluster_id}}
{%             endif %}
set groups {{cfg_group}} protocols bgp group {{contrail_asn_group}} local-address {{bgp.get('ip_address')}}
set groups {{cfg_group}} protocols bgp group {{contrail_asn_group}} hold-time {{bgp.get('hold_time')}}
set groups {{cfg_group}} protocols bgp group {{contrail_asn_group}} multipath
set groups {{cfg_group}} protocols bgp group {{contrail_asn_group}} vpn-apply-export
{%             for family in bgp.get('families', []) %}
{%                 if family.startswith('inet') %}
{%                     set mode='unicast' %}
{%                 elif family == 'evpn' %}
{%                     set mode='signaling' %}
{%                 else %}
{%                     set mode='' %}
{%                 endif %}
set groups {{cfg_group}} protocols bgp group {{contrail_asn_group}} family {{family}} {{mode}} 
{%             endfor %}
{%             if bgp.get('type_') == 'internal' %}
{%                 for family in bgp.get('families', []) %}
{%                     if family.startswith('inet') %}
set groups {{cfg_group}} policy-options policy-statement _contrail_ibgp_export_policy term {{family}} from family {{family}}
set groups {{cfg_group}} policy-options policy-statement _contrail_ibgp_export_policy term {{family}} then next-hop self
set groups {{cfg_group}} protocols bgp group {{contrail_asn_group}} export _contrail_ibgp_export_policy
{%                     endif %}
{%                 endfor %}
{%             endif %}
{%         endif %}
{%     endfor %}
